export var baseUrl = 'https://prayer.jiangkukeji.cn/api';
console.log(baseUrl);

export const http = (url, data, method = 'GET') => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: baseUrl + url,
      data,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'token': uni.getStorageSync('token') || ''
        // 'token': 'f79f0cdd-348d-4e75-b0a7-90d8ce835ca3'
      },
      method,
      success: (res) => {
        // console.log(url,data,res,"接口")
        if (res.data.code == 1) { //请求成功
          resolve(res.data)
        } else if (res.data.code == 401) {
          uni.removeStorage({
            key: 'token'
          })
          uni.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          toLogin();
        } else {
          uni.showToast({
            title: res.data.msg,
            icon: 'none'
          })

        }
      },
      fail: (res) => {
        // uni.showToast({
        // 	title: '网络链接错误，刷新下',
        // 	icon: 'none'
        // })
      },
      complete: () => {

      }
    });
  })
}

function toLogin(e) {
  uni.hideLoading()

  uni.clearStorageSync();
  uni.reLaunch({
    url: "/pages/login/login"
  })
}